package com.ebb;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.kinvey.KCSClient;
import com.kinvey.KinveySettings;
import com.kinvey.KinveyUser;
import com.kinvey.MappedAppdata;
import com.kinvey.push.KCSPush;
import com.kinvey.push.KCSPushNotificationBuilder;
import com.kinvey.push.KCSPushReceiver;
import com.kinvey.util.ListCallback;
import com.kinvey.util.ScalarCallback;

public class EBBKinveyActivityList extends Activity implements OnClickListener {
	private EditText etSave;
	private EditText etsearch;
	private Button btnSave;
	private Button btnSearch;
	private Button btnLogout;
	private TableLayout listTable;
	private KCSClient client;
	private String msgResponse;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);

        this.client = ((EBBKinveyApplication) getApplication()).getKinveyService();
        
		initLayout();
		
		logoutCurrntUser();
		
		loadTableList();
	}

	private void initLayout() {
		this.etSave = (EditText) findViewById(R.id.save_text);
		this.etsearch = (EditText) findViewById(R.id.search_text);
		
		this.btnSave = (Button) findViewById(R.id.save_button);
		this.btnSearch = (Button) findViewById(R.id.search_button);
		this.btnLogout = (Button) findViewById(R.id.logout_button);
		
		this.listTable = (TableLayout) findViewById(R.id.list_table);
		
		this.btnSave.setId(0x3001);
		this.btnSearch.setId(0x3002);
		this.btnLogout.setId(0x3003);

		this.btnSave.setOnClickListener(this);
		this.btnSearch.setOnClickListener(this);
		this.btnLogout.setOnClickListener(this);
	}
	
	private void logoutCurrntUser() {
		KinveyUser currentUser = client.getCurrentUser();
		if(currentUser == null) {
			Log.d("currentUser ::", "currentUser is null");
		} else {
			Log.d("currentUser ::", "currentUser is not null");
			currentUser.logout();
		}
	}

	private void loadTableList() {
		listTable.removeAllViewsInLayout();

		// get object list
		MappedAppdata mappedData = new MappedAppdata(client, "bbs");
		mappedData.fetch(EBBBBSMapper.class, new ListCallback<EBBBBSMapper>() {
			@Override
			public void onSuccess(List<EBBBBSMapper> list) {
				Log.d("loadTableList onSuccess :::::", "list size : " + list.size());
				for (EBBBBSMapper bbsObject : list) {
					addTableRow(bbsObject);
				}
			}

			@Override
			public void onFailure(Throwable e) {
				KinveyUser currentUser = client.getCurrentUser();
				Log.d("currentUser getUsername :::::", currentUser.getUsername());
				Log.d("loadTableList ERROR :::::", e.getMessage());
				super.onFailure(e);
				e.printStackTrace();
			}
		});
	}

	private void addTableRow(final EBBBBSMapper bbsObject) {
		Log.d("addTableRow :::::", bbsObject.toString());

		TableRow tr = new TableRow(this);
		String date = "";
		// try {
		// date = bbsObject.getKmd().get("lmt").toString();
		// } catch (JSONException e) {
		// e.printStackTrace();
		// }

		// date
		TextView tv = new TextView(this);
		tv.setText(date);
		tv.setPadding(5, 5, 5, 5);
		tr.addView(tv);

		// content
		tv = new TextView(this);
		tv.setPadding(5, 5, 5, 5);
		tv.setText(bbsObject.getContent());

		tr.addView(tv);

		tr.setOnClickListener(new OnClickListener() {
			private String objectId = bbsObject.getId();

			@Override
			public void onClick(View v) {
				Log.d(">>>>>>>>>>>>>>>> objceId >>>>>>>>>>>>>>>>>>>>>", objectId);
				Intent i = new Intent("com.ebb.EBBKinveyActivity3");
				i.putExtra("objectId", objectId);
				startActivity(i);
			}
		});

		listTable.addView(tr);
	}

	/**
	 * Display a simple alert dialog with the given text and title.
	 * 
	 * @param context
	 *            Android context in which the dialog should be displayed
	 * @param title
	 *            Alert dialog title
	 * @param text
	 *            Alert dialog message
	 */
	protected static void showAlert(Context context, String title, String text) {
		Builder alertBuilder = new Builder(context);
		alertBuilder.setTitle(title);
		alertBuilder.setMessage(text);
		alertBuilder.create().show();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case 0x3001: // Save
			EBBBBSMapper bbsObject = new EBBBBSMapper();

			bbsObject.setContent(etSave.getText().toString());
			client.mappeddata("bbs").save(bbsObject, new ScalarCallback<Void>() {
				@Override
				public void onSuccess(Void r) {
					msgResponse = "Echo!";
					Toast.makeText(getBaseContext(), msgResponse, Toast.LENGTH_LONG).show();
					etSave.setText("");
					loadTableList();
				}

				@Override
				public void onFailure(Throwable e) {
					e.printStackTrace();
					super.onFailure(e);
				}
			});
			break;
		case 0x3002:  // Search
			break;
		case 0x3003: // Logout
			logoutCurrntUser();
			msgResponse = "Logout";
			Toast.makeText(getBaseContext(), msgResponse, Toast.LENGTH_LONG).show();
			Intent i = new Intent("com.ebb.EBBKinveyActivityMain");
			startActivity(i);
			break;
		}
	}
}